package com.xuef.dxf.namenode.server;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

public class NameNodeRpcServer {
    private static final int DEFAULT_PORT = 50070;
    private int PORT = DEFAULT_PORT;
    FSNameSystem fsNameSystem;
    DataNodeManager dataNodeManager;
    public NameNodeRpcServer(FSNameSystem fsNameSystem,
                             DataNodeManager dataNodeManager) {
        this.fsNameSystem = fsNameSystem;
        this.dataNodeManager = dataNodeManager;
    }

    public void start() throws IOException, InterruptedException {
        // Create a new server to listen on port 50070
        Server server = ServerBuilder.forPort(PORT)
                .addService(new NameNodeRpcServiceImpl(fsNameSystem, dataNodeManager))
                .build();


        // Start the server
        server.start();

        // Server threads are running in the background.
        System.out.println("Server started, Listen on port: " + this.PORT);
        // Don't exit the main thread. Wait until server is terminated.
        server.awaitTermination();
    }
}
